﻿using System.Configuration;
using Microsoft.SharePoint.Utilities;

namespace WoltersKluwer.SPChainGang.Core.Configuration
{
    public class ConfigurationSettings
    {
        private readonly System.Configuration.Configuration config;
        readonly TemplatesConfigurationSection templates;

        public ConfigurationSettings()
        {
            var fileMap = new ExeConfigurationFileMap
                              {
                                  ExeConfigFilename =
                                      string.Format(
                                          @"{0}\WK\SPChainGang.config",
                                          SPUtility.GetGenericSetupPath(@"Template\LAYOUTS"))
                              };

            config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            if (config == null)
                throw new ConfigurationErrorsException(
                    string.Format("Failed to initialize properly. Check the file exists/accessable at {0}", fileMap.ExeConfigFilename));

            templates = (TemplatesConfigurationSection)config.GetSection("templates");
            if (templates == null)
                throw new ConfigurationErrorsException("Failed to load the templates section");
        }

        public string MailBodyTemplate
        {
            get
            {
                var template = templates.Templates.GetByName("Mail.Body");
                return template != null ? template.Value : null;
            }
        }

        public string BrokenLinksCheckUsername
        {
            get
            {
                return config.AppSettings.Settings["BrokenLinksCheck.Username"].Value;
            }
        }

        public string BrokenLinksCheckPassword
        {
            get
            {
                return config.AppSettings.Settings["BrokenLinksCheck.Password"].Value;
            }
        }

        public string BrokenLinksCheckDomain
        {
            get
            {
                return config.AppSettings.Settings["BrokenLinksCheck.Domain"].Value;
            }
        }

        public string MailSubject
        {
            get
            {
                return config.AppSettings.Settings["Mail.Subject"].Value;
            }
        }

        public string FTPHost
        {
            get
            {
                return config.AppSettings.Settings["FTP.Host"].Value;
            }
        }

        public string FTPUserName
        {
            get
            {
                return config.AppSettings.Settings["FTP.Username"].Value;
            }
        }

        public string FTPPassword
        {
            get
            {
                return config.AppSettings.Settings["FTP.Password"].Value;
            }
        }

        public bool UpdateExternalLinksOnly
        {
            get
            {
                bool updateExternalLinksOnly;
                bool.TryParse(config.AppSettings.Settings["UpdateExternalLinksOnly"].Value,out updateExternalLinksOnly);
                return updateExternalLinksOnly;
            }
        }
    }

}
